<?php
namespace App\Controller;

/**
 * 伐木累活动
 * @author liubo <liubo@ilongyuan.com.cn>
 */
class FamuleiController extends AppController
{
    public function _initialize() {
        parent::_initialize();

        $this->prefix = "#66赢大奖#";
        $this->game_id = 8; // 守望先锋
        $this->start = strtotime("2016-07-07 00:00:00");
    }

    public function index() {
        $p = I('p', 1, 'intval');
        $p < 1 && $p = 1;
        $limit     = I('limit', IS_MOBILE ? 8 : 12, 'intval');

        $videos = $this->_video($this->prefix, $p, $limit);

        if (IS_AJAX) {
            $this->ajaxReturn(['state' => 0, 'data' => $videos]);
        }
        $this->assign('winningVideos', $this->_winningVideos());
        $this->assign('videos', $videos);
        $this->assign('limit', $limit);
        $this->assign('title', "66赢大奖 OW【66Play】一键记录你的游戏青春");
        if (IS_MOBILE) {
            $token = I('appToken');
            if (!empty($token)) {
                $this->assign('isFromApp', true);
            }
            return $this->display('mobile');
        }
        $total = $this->_count($this->prefix);
        $this->assign('total', $total);
        $this->display();
    }

    private function _video($prefix, $p = 1, $limit = 12)
    {
        $service = D('Video', 'Service');

        $map['title'] = new \MongoRegex("/^$prefix/i");
        $map['status'] = $service::STATUS_PASS;
        $map['type'] = $service::TYPE_NORMAL;
        $map['race_id'] = ['$eq' => null];
        $map['game_id'] = $this->game_id;

        !empty($this->start) && $map['create_time']['$gte'] = $this->start;

        $res = $service->where($map)->field('id')
                       ->order('create_time desc')
                       ->limit(($p-1)*$limit, $limit)
                       ->select();

        $vEvent = event('video');
        $fieldMap = ['id', 'title', 'create_time', 'time', 'user', 'statis', 'uri', 'pre_uri', 'thumb'];
        $list = [];
        foreach ($res as $value) {
            $vid = $value['id'];
            $video = $vEvent->detail($vid);
            $video = $this->field_filter($video, $fieldMap);
            $list[] = $video;
        }
        return $list;
    }

    private function _count($prefix) {
        $service = D('Video', 'Service');

        $map['title'] = new \MongoRegex("/^$prefix/i");
        $map['status'] = $service::STATUS_PASS;
        $map['type'] = $service::TYPE_NORMAL;
        $map['race_id'] = ['$eq' => null];
        $map['game_id'] = $this->game_id;

        !empty($this->start) && $map['create_time']['$gte'] = $this->start;

        return $service->where($map)->count();
    }

    private function _winningVideos() {
        $ids = C('FAMULEI_WINNING_VIDEOS');
        if (empty($ids)) {
            return [];
        }
        $date_list = explode('|', $ids);
        $data = [];
        foreach ($date_list as $key => $value) {
            $arr = explode(':', $value);
            $data[] = [
                'date' => $arr[0],
                'videos' => explode(',', $arr[1]),
            ];
        }
        // echo json_encode($data);exit;
        $vEvent = event('Video');
        foreach ($data as &$value) {
            foreach ($value['videos'] as &$video) {
                $video = $vEvent->detail($video, 0, false);
            }
        }
        return $data;
    }
}